/*
 * @Author: dgflash
 * @Date: 2021-07-03 16:13:17
 * @LastEditors: dgflash
 * @LastEditTime: 2023-01-19 14:52:40
 */
import { CCBoolean } from "cc";
import { Widget } from "cc";
import { Component, math, ResolutionPolicy, screen, UITransform, view, _decorator } from "cc";
const { ccclass, property } = _decorator;

/** 游戏界面屏幕自适应管理 */
@ccclass('GUI')
export class GUI extends Component {

    /** 界面层矩形信息组件 */
    @property(UITransform)
    transform: UITransform;
    /** 是否为竖屏显示 */
    portrait!: boolean;

    /** 竖屏设计尺寸 */
    private portraitDrz: math.Size = null!;
    /** 横屏设计尺寸 */
    private landscapeDrz: math.Size = null!;

    onLoad() {
        this.init();
    }

    /** 初始化 */
    protected init() {

        // if (view.getDesignResolutionSize().width > view.getDesignResolutionSize().height) {
        //     this.landscapeDrz = view.getDesignResolutionSize();
        //     this.portraitDrz = new math.Size(this.landscapeDrz.height, this.landscapeDrz.width);
        // }
        // else {
        //     this.portraitDrz = view.getDesignResolutionSize();
        //     this.landscapeDrz = new math.Size(this.portraitDrz.height, this.portraitDrz.width);
        // }

        // this.resize();

    }

    /** 游戏画布尺寸变化 */
    resize() {
        let dr;
        if (view.getDesignResolutionSize().width > view.getDesignResolutionSize().height) {
            dr = this.landscapeDrz;
        }
        else {
            dr = this.portraitDrz
        }

        var s = screen.windowSize;
        var rw = s.width;
        var rh = s.height;
        var finalW = rw;
        var finalH = rh;
        var uiW = rw;
        var uiH = rh;

        if ((rw / rh) > (dr.width / dr.height)) {
            // 如果更长，则用定高
            finalH = dr.height;
            finalW = finalH * rw / rh;

            uiH = dr.height;
            uiW = (uiH / dr.height) * dr.width;

            this.portrait = false;

            // 手工修改canvas和设计分辨率，这样反复调用也能生效。
            view.setDesignResolutionSize(finalW, finalH, ResolutionPolicy.UNKNOWN);
            this.transform!.width = uiW;
            this.transform!.height = uiH;
        }
        else {
            // 如果更短，则用定宽
            finalW = dr.width;
            finalH = rw / rh * finalW;

            uiW = dr.width;
            uiH = (uiW / dr.width) * dr.height;
            
            this.portrait = true;

            // 手工修改canvas和设计分辨率，这样反复调用也能生效。
            view.setDesignResolutionSize(finalW, finalH, ResolutionPolicy.UNKNOWN);
            this.transform!.width = finalW;
            this.transform!.height = finalH;
        }

    }
}